package com.fireline.beauty.seting;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

import com.fireline.beauty.jdbc.SqliteJdbcImpl;
import com.zgw.fireline.base.controls.DataTable;
import com.zgw.fireline.base.controls.DataTableColumn;
import com.zgw.fireline.base.dataset.Dataset;
import com.zgw.fireline.base.frame.FunctionDialog;
import com.zgw.fireline.base.frame.IFunction;
import com.zgw.fireline.base.widgets.Batch;
import com.zgw.fireline.base.widgets.BatchItem;

/**
 * 会卡员卡设置
 * */
public class HykszView implements IFunction {
	/**
	 * @wbp.nonvisual location=17,372
	 */
	private final Dataset hykSet = new Dataset(true, "hykxx",
			SqliteJdbcImpl.INSTANCE);

	private Control content;
	/**
	 * @wbp.nonvisual location=13,432
	 */
	private final Batch delFwxm = new Batch(SqliteJdbcImpl.INSTANCE);
	private DataTable dataTable;

	/**
	 * @wbp.parser.entryPoint
	 */
	@Override
	public Control createControl(Composite parent) {
		hykSet.setKeyColumns(new String[] {});

		BatchItem batchItem = new BatchItem(delFwxm);
		batchItem.setCommand("delete from hykxx where id=?");
		batchItem.setName("删除会员卡");
		Composite content = new Composite(parent, SWT.NONE);
		content.setLayout(new GridLayout(1, false));

		ToolBar toolBar = new ToolBar(content, SWT.FLAT | SWT.RIGHT);
		toolBar.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		ToolItem tltmNewItem = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem.setText("增加");
		tltmNewItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doAdd();
			}
		});
		ToolItem tltmNewItem_1 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doEdit();
			}
		});
		tltmNewItem_1.setText("修改");

		ToolItem tltmNewItem_2 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_2.setText("删除");
		tltmNewItem_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doRemove();
			}
		});

		ToolItem tltmNewItem_6 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_6.setText("退出");

		Label label = new Label(content, SWT.SEPARATOR | SWT.HORIZONTAL);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));

		dataTable = new DataTable(content, SWT.BORDER | SWT.FULL_SELECTION,
				hykSet);
		dataTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1,
				1));
		dataTable.setHeaderVisible(true);
		dataTable.setLinesVisible(true);

		DataTableColumn dataTableColumn_9 = new DataTableColumn(dataTable,
				SWT.NONE, "hykmc");
		dataTableColumn_9.setText("会员卡名称");
		dataTableColumn_9.setWidth(100);

		DataTableColumn dataTableColumn_10 = new DataTableColumn(dataTable,
				SWT.RIGHT, "knje");
		dataTableColumn_10.setText("卡内金额");
		dataTableColumn_10.setWidth(80);

		DataTableColumn dataTableColumn_11 = new DataTableColumn(dataTable,
				SWT.RIGHT, "sj");
		dataTableColumn_11.setText("售价");
		dataTableColumn_11.setWidth(80);

		DataTableColumn dataTableColumn_12 = new DataTableColumn(dataTable,
				SWT.NONE, "fwzk");
		dataTableColumn_12.setText("服务折扣");
		dataTableColumn_12.setWidth(80);

		DataTableColumn dataTableColumn_13 = new DataTableColumn(dataTable,
				SWT.NONE, "cpzk");
		dataTableColumn_13.setText("产品折扣");
		dataTableColumn_13.setWidth(80);

		DataTableColumn dataTableColumn_5 = new DataTableColumn(dataTable,
				SWT.NONE, "jy");
		dataTableColumn_5.setText("禁用");
		dataTableColumn_5.setWidth(50);

		DataTableColumn dataTableColumn_8 = new DataTableColumn(dataTable,
				SWT.NONE, "bz");
		dataTableColumn_8.setText("备注");
		dataTableColumn_8.setWidth(150);
		setParams();
		createGUIAfter();
		this.content = content;
		return content;
	}

	protected void doRemove() {
		if (hykSet.getSelectedIndex() != -1) {
			if (MessageDialog.openQuestion(content.getShell(), "操作提示", "确认删除 "
					+ hykSet.getValue("xmmc", String.class) + "?")) {
				setParams();
				delFwxm.execute();
				doSearch();// 刷新数据
			}
		} else {
			MessageDialog.openInformation(content.getShell(), "操作提示",
					"请选择需要删除的记录");
		}
	}

	protected void doAdd() {
		HykEdit edit = new HykEdit(HykEdit.Add, null);
		FunctionDialog dlg = new FunctionDialog(content.getShell(), edit);
		dlg.open();
		if (edit.resultCode == SWT.OK)
			doSearch();
	}

	protected void doEdit() {
		HykEdit edit = new HykEdit(HykEdit.Edit, hykSet); 
		FunctionDialog dlg = new FunctionDialog(content.getShell(), edit);
		dlg.open();
		if (edit.resultCode == SWT.OK)
			doSearch();
	}

	protected void doSearch() {
		setParams();
		hykSet.update();
	}

	protected void setParams() {
		delFwxm.setParam(0, 0, hykSet.getValue("id"));
	}

	protected void createGUIAfter() {
		hykSet.update();
	}

	// 测试方法
	public static void main(String[] args) {
		FunctionDialog dlg = new FunctionDialog(new Shell(), new HykszView());
		dlg.open();
	}
}
